﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace FastUtil.Convert
{
    public class StructConvert
    {
        public static List<T> ConvertArrayToList<T>(T[] BeConvertArray)
        {
            return new List<T>(BeConvertArray);
        }
        public static string ConvertEnity2InnerXml<T>(T Entity)
        {
            string XMLStr = ConvertEnity2Xml(Entity);
            XmlDocument Doc = new XmlDocument();
            Doc.LoadXml(XMLStr);
            XmlNode AttributeNode = Doc.SelectSingleNode(Entity.GetType().Name);
            XmlNode Node = Doc.CreateElement("Node");
            for (int i = 0; i < AttributeNode.ChildNodes.Count; i++)
            {
                XmlNode ChildNode = AttributeNode.ChildNodes[i];
                XmlNode CopyNode = Doc.CreateElement(ChildNode.Name);
                CopyNode.InnerText = ChildNode.InnerText;
                Node.AppendChild(CopyNode);
            }
            return Node.InnerXml;
        }
        public static string ConvertEnity2Xml<T>(T Entity)
        {
            try
            {
                using (StringWriter sw = new StringWriter())
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    serializer.Serialize(sw, Entity);
                    sw.Close();
                    return sw.ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("将实体对象转换成XML异常", ex);
            }

        }
        public static string ConvertXml2String(XmlDocument Doc)
        {
            MemoryStream stream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, null);
            writer.Formatting = Formatting.Indented;
            Doc.Save(writer);
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
            stream.Position = 0;
            string xmlString = sr.ReadToEnd();
            sr.Close();
            stream.Close();
            return xmlString;
        }
        public static T ConvertXMLNode2Entity<T>(XmlNode Node) where T : class
        {
            Type EntityType = typeof(T);
            Object obj = EntityType.GetConstructors()[0].Invoke(new Object[] { });
            PropertyInfo[] PropertyList = EntityType.GetProperties();
            foreach (PropertyInfo P in PropertyList)
            {
                XmlNode ChildNode = Node.SelectSingleNode(P.Name);
                if (ChildNode != null)
                {
                    Object val = null;
                    if (!string.IsNullOrEmpty(ChildNode.InnerXml))
                    {
                        if (P.PropertyType.Name == "String")
                        {
                            val = ValueConvert.ConvertToString(ChildNode.InnerXml);
                        }
                        else if (P.PropertyType.Name == "DateTime")
                        {
                            val = ValueConvert.ConvertToDateTime(ChildNode.InnerXml);
                        }
                        else if (P.PropertyType.Name == "Int32")
                        {
                            val = ValueConvert.ConvertToInt32(ChildNode.InnerXml);
                        }
                        else if (P.PropertyType.Name == "Double")
                        {
                            val = ValueConvert.ConvertToDouble(ChildNode.InnerXml);
                        }
                        else if (P.PropertyType.Name == "Decimal")
                        {
                            val = ValueConvert.ConvertToDecimal(ChildNode.InnerXml);
                        }
                    }
                    P.SetValue(obj, val, null);
                }
            }
            return (T)obj;

        }
    }
}
